<div class="filter">
    <div class="item">筛选：</div>
    <div class="f1 item" style="width: 100px;">
        <select>
            <option value="-1">所有状态</option>
            <option value="0">正常</option>
            <option value="1">冻结</option>
            <option value="2">停用</option>
        </select>
    </div>
    <div class="item">搜索：</div>
    <div class="f2 item" style="width: 80px;">
        <select>
            <option value="uid">账号</option>
            <option value="name">姓名</option>
            <option value="qq">QQ</option>
            <option value="ip">登录IP</option>
        </select>
    </div>

    <div class="f3 item" style="width: 140px;">
        <input type="text" class="am-form-field" placeholder="" />
    </div>
    <div class="f4 item" style="width: 140px;">
        <input type="number" class="am-form-field" placeholder="未登录天数" />
    </div>
    
    <div class="button item">
        <button type="button" class="am-btn am-btn-primary">查询</button>
    </div>
    <div class="item">
        <button type="button" class="am-btn am-btn-primary add-btn">新增代理</button>
    </div>
</div>

<table class="am-table am-table-bordered am-table-radius am-table-striped am-table-compact" id="result-table" 
    style="min-width: 800px;">
    <thead>
    <tr>
        <th>在线</th>
        <th>代理</th>
        <th>会员数</th>
        <th><i class="am-icon-sort-numeric-as hide" value='balance'></i><i class="am-icon-sort-numeric-desc" value='balanceDesc'></i> 可用额度</th>
        <th>状态</th>
        <th>代理模式</th>
        <th><i class="am-icon-sort-numeric-asc hide" value="register"></i><i class="am-icon-sort-numeric-desc active" value="registerDesc"></i>  新增时间</th>
        <th><i class="am-icon-sort-numeric-asc hide" value="login"></i><i class="am-icon-sort-numeric-desc" value="loginDesc"></i> 最后活动时间</th>
        <th>未登录</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    </tbody>
</table>

<div class="zxf_pagediv"></div>

<style type="text/css">
    #result-table td {
        padding: 6px 2px 6px 2px;
        line-height: 20px;
        text-align: center;
    }
    #result-table td button {
        border-radius: 3px;
        padding-left: 2px;
        padding-right: 2px;
        background: #fff;
        color: #000;
    }
    #result-table .count {
        display: inline-block;
        cursor: pointer;
        background-color: #0e90d2;
        line-height: 20px;
        padding: 0px 4px 0px 4px;
        border-radius: 8px;
        color: #fff;
        font-weight: bold;
    }
    #result-table i {
        cursor: pointer;
    }
    #result-table i.active {
        color: red;
    }
    i.hide {
        display: none;
    }
</style>

<script>
    Util.restorePageFilter(function(extra) {
        var sort = extra[0];
        _setSortActive(sort, true);
    });
    var _f = Util.queryTemplate($("#result-table tbody"), ".zxf_pagediv", $(".filter .button button"), function(page, size, cb) {
        var data = {
            minGrade: 49,
            maxGrade: 49,
        };
        var state = $(".f1 select").val();
        if (state != -1) data.state = state;
        var unknown = $(".f3 input").val().trim();
        if (unknown) {
            data[$(".f2 select").val()] = unknown;
        }

        // 未登录天数
        var v = $(".f4 input").val().trim();
        if (v) {
            var t = new Date().getTime() - v * 24 * 3600 * 1000;
            data.maxLogin = t;
        }

        // 排序
        var sort = $("#result-table i.active").attr("value");
        data.sort = sort;

        Util.savePageFilter([".f1 select", ".f3 input", ".f2 select", ".f4 input"], [sort]);
        startLoading();
        CMD.get_user_list(data, page, size, function(r) {
            stopLoading();
            cb(r);
            registerOpers(r.list);
        });
    }, function(table, list, sum) {
        if (Util.isEmpty(list)) {
            table.append('<tr><td colspan="10">暂时没有数据</td></tr>');
            return;
        }

        for (var i in list) {
            var d = list[i];
            var tr = $("<tr value='" + d.mobile + "'></tr>");
            if (d.online == 1)
                tr.append('<td><i class="am-icon-circle" style="color:blue"></i></td>');
            else
                tr.append('<td><i class="am-icon-circle" style="color:gray"></i></td>');
            
            tr.append("<td>" + d.mobile + "</td>");
            var l = "<td><span class='count' value='__v__'>__c__</span></td>".replace(/__v__/, d.mobile);
            l = l.replace(/__c__/, d.refereeCount || 0);
            tr.append(l);
            tr.append("<td>" + (d.balance/100) + "</td>");

            // 状态
            if (d.state == 0) tr.append("<td>正常</td>");
            else if (d.state == 1) tr.append("<td>冻结</td>");
            else if (d.state == 2) tr.append("<td>停用</td>");

            if (d.refereeType != 1)
                tr.append("<td>分红</td>");
            else
                tr.append("<td>赔率差</td>");
            tr.append('<td>' + new Date(d.register).Format("yyyy-MM-dd HH:ss:mm") + '</td>');

            if (d.login) {
                tr.append('<td>' + new Date(d.login).Format("yyyy-MM-dd HH:ss:mm") + '</td>');
                // 未登录天数
                var t = (new Date().getTime() - new Date(d.login).getTime())/(1000 * 24 * 3600);
                t = parseInt(t);
                tr.append("<td>" + t + "</td>");
            }
            else {
                tr.append("<td></td>");
                tr.append("<td></td>");
            }

            // 操作
            var oper = '\
                <button class="edit" value="__v__">修改</button> \
                <button class="detail" value="__v__">查看详情</button> \
                <button class="modify_balance" value="__v__">修改余额</button> \
                <button class="balance" value="__v__">资金明细</button>';
            if (d.refereeType == 1)
                oper += '<button class="restore_lottery" value="__v__">设置盘口</button>';
            else
                oper += '<button class="bonus_rate" value="__v__">分红比例</button>';
            oper += '<button class="delete" value="__v__">删除</button>';
            oper = oper.replace(/__v__/g, d.mobile);
            tr.append("<td>" + oper + "</td>");
            table.append(tr);
        }

        // 会员数被点击，列出此代理的会员列表
        $("#result-table .count").click(function(e) {
            var uid = $(e.currentTarget).attr("value");
            for (var i in list) {
                if (list[i].mobile == uid) {
                    // 跳转到会员列表
                    context.center.referee = list[i].mobile;
                    context.center.refereeOper = 'list';
                    PageHistory.load("用户管理", "会员", 0);
                    return;
                }
            }
        });
    });

    // 排序的操作
    $("#result-table i").click(function(e) {
        var target = $(e.currentTarget);
        var arr = target.parent().find("i");
        var sort = target.attr("value");
        if (target.attr("class").indexOf("active") != -1) {
            // 需要切换下升序、降序
            target.removeClass("active");
            target.hide();
            for (var i = 0; i < arr.length; i++) {
                if ($(arr[i]).attr("value") != target.attr("value")) {
                    $(arr[i]).show();
                    $(arr[i]).addClass("active");
                    sort = $(arr[i]).attr("value");
                }
            }
        }
        _setSortActive(sort);
    });
    function _setSortActive(sort, first) {
        $("#result-table i").removeClass("active");
        var target = $("#result-table i[value='" + sort + "']");
        target.addClass("active");
        target.show();
        var arr = target.parent().find("i");
        for (var i = 0; i < arr.length; i++) {
            if ($(arr[i]).attr("value") != sort) {
                $(arr[i]).hide();
            }
        }
        console.log("排序", sort);
        if (!first) _f();
    }

    // 注册操作项的处理
    function registerOpers(list) {
        var findUser = function(uid) {
            for (var i in list) {
                if (list[i].mobile == uid) return list[i];
            }
        };

        $("#result-table button.edit").click(function(e) {
            var d = findUser($(e.currentTarget).attr("value"));
            if (!d) return;

            context.center.referee = d;
            context.center.refereeOper = 'edit';
            parent._f = _f;
            Util.addPopUp("modify_daili_info", "modify_daili_info", 390);
        });

        $("#result-table button.detail").click(function(e) {
            var d = findUser($(e.currentTarget).attr("value"));
            if (!d) return;

            parent._f = _f;
            context.center.referee = d;
            context.center.refereeOper = 'detail';
            Util.addPopUp("modify_daili_info", "modify_daili_info", 390);
        });

        $("#result-table button.modify_balance").click(function(e) {
            var d = findUser($(e.currentTarget).attr("value"));
            if (!d) return;

            context.center.user = d;
            PageHistory.load("财务管理", "余额变更", 0);
        });

        $("#result-table button.balance").click(function(e) {
            var d = findUser($(e.currentTarget).attr("value"));
            if (!d) return;

            context.center.user = d;
            context.center.balanceOper = 'seeOne';
            PageHistory.load("财务管理", "账变记录", 0);
        });

        $("#result-table button.delete").click(function(e) {
            var d = findUser($(e.currentTarget).attr("value"));
            if (!d) return;

            if (confirm("确定删除会员：" + d.mobile + "吗？")) {
                CMD.user_state(d.id, 2, function() {
                    _f();
                });
            };
        });

        $("#result-table button.restore_lottery").click(function(e) {
            var d = findUser($(e.currentTarget).attr("value"));
            if (!d) return;
            context.center.lotteryOper = "edit";
            context.center.user = d;
            PageHistory.load("游戏管理", "盘口设定", 0);
        });

        $("#result-table button.bonus_rate").click(function(e) {
            var d = findUser($(e.currentTarget).attr("value"));
            if (!d) return;
            context.center.refereeBonus = 'referee';
            context.center.refereeInfo = d;
            PageHistory.load("代理结算", "代理结算配置", 0);
        });
    }

    // 新增代理
    $(".item .add-btn").click(function() {
        context.center.refereeOper = 'add';
        parent._f = _f;
        Util.addPopUp("add_daili", "add_daili", 390);
    })
</script>
